package com.aimaterials.resource.controller;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import com.aimaterials.common.core.domain.entity.SysUser;
import com.aimaterials.common.core.domain.model.LoginUser;
import com.aimaterials.common.utils.ServletUtils;
import com.aimaterials.framework.web.service.TokenService;
import com.aimaterials.resource.util.MathUtils;
import com.aimaterials.resource.util.QRCodeGenerator;
import com.google.zxing.WriterException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.aimaterials.common.annotation.Log;
import com.aimaterials.common.core.controller.BaseController;
import com.aimaterials.common.core.domain.AjaxResult;
import com.aimaterials.common.enums.BusinessType;
import com.aimaterials.resource.domain.ResStereoscopic;
import com.aimaterials.resource.service.IResStereoscopicService;
import com.aimaterials.common.utils.poi.ExcelUtil;
import com.aimaterials.common.core.page.TableDataInfo;

/**
 * 立体教材Controller
 * 
 * @author DaemonSu
 * @date 2020-11-10
 */
@RestController
@RequestMapping("/resource/stereoscopic")
public class ResStereoscopicController extends BaseController
{
    @Autowired
    private IResStereoscopicService resStereoscopicService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询立体教材列表
     */
    @PreAuthorize("@ss.hasPermi('resource:stereoscopic:list')")
    @GetMapping("/list")
    public TableDataInfo list(ResStereoscopic resStereoscopic)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        if(!user.isAdmin()){
            resStereoscopic.setDeptId(user.getDeptId());
        }
//        resStereoscopic.setDeptId(null);
        startPage();
        List<ResStereoscopic> list = resStereoscopicService.selectResStereoscopicList(resStereoscopic);
        return getDataTable(list);
    }

    /**
     * 查询所有书籍列表，但具有是否在dept中选中标识
     */
    @PreAuthorize("@ss.hasPermi('resource:stereoscopic:list')")
    @GetMapping("/listALLNoPage")
    public TableDataInfo listALLNoPage(ResStereoscopic resStereoscopic)
    {
        List<ResStereoscopic> list = resStereoscopicService.selectResStereoscopicList(resStereoscopic);
        return getDataTable(list);
    }

    /**
     * 查询立体教材列表
     */
//    @PreAuthorize("@ss.hasPermi('resource:stereoscopic:list')")
    @GetMapping("/listInDept")
    public TableDataInfo listInDept(ResStereoscopic resStereoscopic)
    {
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        SysUser user = loginUser.getUser();
        startPage();
        List<ResStereoscopic> list = resStereoscopicService.selectResStereoscopicList(resStereoscopic);
        return getDataTable(list);
    }

    /**
     * 导出立体教材列表
     */
    @PreAuthorize("@ss.hasPermi('resource:stereoscopic:export')")
    @Log(title = "立体教材", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ResStereoscopic resStereoscopic)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        resStereoscopic.setDeptId(user.getDeptId());
        List<ResStereoscopic> list = resStereoscopicService.selectResStereoscopicList(resStereoscopic);
        ExcelUtil<ResStereoscopic> util = new ExcelUtil<ResStereoscopic>(ResStereoscopic.class);
        return util.exportExcel(list, "stereoscopic");
    }

    /**
     * 获取立体教材详细信息
     */
    @PreAuthorize("@ss.hasPermi('resource:stereoscopic:query')")
    @GetMapping(value = "/{stereoscopicId}")
    public AjaxResult getInfo(@PathVariable("stereoscopicId") Long stereoscopicId)
    {
        return AjaxResult.success(resStereoscopicService.selectResStereoscopicById(stereoscopicId));
    }

    /**
     * 新增立体教材
     */
    @PreAuthorize("@ss.hasPermi('resource:stereoscopic:add')")
    @Log(title = "立体教材", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ResStereoscopic resStereoscopic)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        resStereoscopic.setDeptId(user.getDeptId());
        return toAjax(resStereoscopicService.insertResStereoscopic(resStereoscopic));
    }

    /**
     * 修改立体教材
     */
    @PreAuthorize("@ss.hasPermi('resource:stereoscopic:edit')")
    @Log(title = "立体教材", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ResStereoscopic resStereoscopic)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        resStereoscopic.setDeptId(user.getDeptId());
        return toAjax(resStereoscopicService.updateResStereoscopic(resStereoscopic));
    }

    /**
     * 删除立体教材
     */
    @PreAuthorize("@ss.hasPermi('resource:stereoscopic:remove')")
    @Log(title = "立体教材", businessType = BusinessType.DELETE)
	@DeleteMapping("/{stereoscopicIds}")
    public AjaxResult remove(@PathVariable Long[] stereoscopicIds)
    {
        return toAjax(resStereoscopicService.deleteResStereoscopicByIds(stereoscopicIds));
    }


    /**
     * 删除学校选用的立体教材
     */
    @PreAuthorize("@ss.hasPermi('resource:stereoscopic:remove')")
    @Log(title = "立体教材", businessType = BusinessType.DELETE)
    @DeleteMapping("/{stereoscopicIds}/{deptId}")
    public AjaxResult removeFromDept(@PathVariable Long[] stereoscopicIds,@PathVariable Long deptId)
    {
        return toAjax(resStereoscopicService.deleteResStereoscopicInDept(stereoscopicIds,deptId));
    }

    /**
     * 保存学校选用的立体教材
     */
    @PreAuthorize("@ss.hasPermi('resource:stereoscopic:remove')")
    @Log(title = "立体教材", businessType = BusinessType.DELETE)
    @PutMapping("/{stereoscopicIds}/{deptId}")
    public AjaxResult saveInDept(@PathVariable Long[] stereoscopicIds,@PathVariable Long deptId)
    {
        return toAjax(resStereoscopicService.saveResStereoscopicInDept(stereoscopicIds,deptId));
    }

    /**
     * 删除立体教材中的资源
     */
    @PreAuthorize("@ss.hasPermi('resource:stereoscopic:remove')")
    @Log(title = "立体教材", businessType = BusinessType.DELETE)
    @DeleteMapping("/res/{resourceIds}/{sterId}")
    public AjaxResult removeRes(@PathVariable Long[] resourceIds,@PathVariable Long sterId)
    {
        return toAjax(resStereoscopicService.deleteResByIds(resourceIds,sterId));
    }


    /**
     * 修改资源的二维码
     */
    @PreAuthorize("@ss.hasPermi('resource:stereoscopic:edit')")
    @Log(title = "立体教材", businessType = BusinessType.UPDATE)
    @PutMapping("/qrcode/{resourceId}/{sterId}/{qrcode}")
    public AjaxResult updateQRCode(@PathVariable("resourceId") Long resourceId,@PathVariable("sterId") Long sterId,@PathVariable("qrcode") String qrcode)
    {
        return toAjax(resStereoscopicService.updateQRCode(resourceId,sterId,qrcode));
    }

    /**
     * 更新立体教材中的资源
     */
    @PreAuthorize("@ss.hasPermi('resource:stereoscopic:edit')")
    @Log(title = "立体教材", businessType = BusinessType.UPDATE)
    @PutMapping("/res/{resourceIds}/{sterId}")
    public AjaxResult updateRes(@PathVariable Long[] resourceIds,@PathVariable Long sterId)
    {
        return toAjax(resStereoscopicService.updateResByIds(resourceIds,sterId));
    }

    /**
     * 根据二维码，加载资源信息
     */
    @PreAuthorize("@ss.hasPermi('resource:stereoscopic:query')")
    @GetMapping("/res/qrquery")
    public AjaxResult getResByQRCode(@RequestParam("qrcode") String qrcode)
    {
        String code=qrcode;
        if(qrcode.length()==28){
            code=MathUtils.transRadix(qrcode.substring(4),45, 16);

        }
        String uuid=hengxian(code).toLowerCase(Locale.ROOT);
        //处理二维码
        return AjaxResult.success(resStereoscopicService.selectResByQrCode(qrcode,uuid));
    }

    /**
     * 给UUID增加横线
     * @param code
     */
    public   String   hengxian(String code){
        StringBuilder sb=new StringBuilder("");
        sb.append(code.substring(0,9)).append("-")
                .append(code.substring(9,13)).append("-")
                .append(code.substring(13,17)).append("-").append(code.substring(17,21)).append("-").append(code.substring(21));
        return sb.toString();
    }

}
